package tanxin;

import java.util.Arrays;
import java.util.Comparator;

public class Beibao {

    public static void main(String[] args) {

        //准备货物
        Item[] items = {
                new Item(35, 10),
                new Item(30, 40),
                new Item(60, 30),
                new Item(50, 50),
                new Item(40, 35),
                new Item(10, 40),
                new Item(25, 30),
        };
        System.out.println("----------按货物重量排序------------");
        packSack(items, new Comparator<Item>() {
            @Override
            public int compare(Item o1, Item o2) {
                return o1.getWeight() - o2.getWeight();
            }
        });

        System.out.println("----------按货物价值排序------------");
        packSack(items, new Comparator<Item>() {
            @Override
            public int compare(Item o1, Item o2) {
                return o2.getValue() - o1.getValue();
            }
        });

        System.out.println("----------按货物价值密度排序------------");
        packSack(items, new Comparator<Item>() {
            @Override
            public int compare(Item o1, Item o2) {
                return Double.compare(o2.getValueDen() , o1.getValueDen());
            }
        });

        System.out.println(1/3);



    }

    static void packSack(Item[] items, Comparator<Item> comp){
        //容量
        int capacity = 500;
        //排序
        Arrays.sort(items, comp);
        for (Item i :
                items) {
            System.out.println(i);
        };
        //放入背包的货物索引
        int nums = 0;
        //货物总价值
        int value = 0;
        while (nums <= items.length-1 && capacity >= items[nums].getWeight() ){

            capacity = capacity - items[nums].getWeight();

            value += items[nums].getValue();

            System.out.println("装第" + (nums+1) + "件货物，货物信息：重量"+ items[nums].getWeight()
                    +"、价值："+items[nums].getValue()+"，剩余容量：" + capacity);

            nums ++;

        }
        System.out.println("货物总价值："+ value);


    }


}
